1 // class template array -*- C++ -*-
3 // Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
31 * This is a TR1 C++ Library header.
41 #include <bits/functexcept.h>
48 /// @brief struct array [6.2.2].
49 /// NB: Requires complete type _Tp.
50 template<typename _Tp, std::size_t _Nm = 1>
53 typedef _Tp value_type;
54 typedef value_type& reference;
55 typedef const value_type& const_reference;
56 typedef value_type* iterator;
57 typedef const value_type* const_iterator;
58 typedef std::size_t size_type;
59 typedef std::ptrdiff_t difference_type;
60 typedef std::reverse_iterator<iterator> reverse_iterator;
61 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
63 // Compile time constant without other dependencies.
64 enum { _S_index = _Nm };
66 // Support for zero-sized arrays mandatory.
67 value_type _M_instance[_Nm ? _Nm : 1] __attribute__((__aligned__));
69 // No explicit construct/copy/destroy for aggregate type.
72 assign(const value_type& __u)
73 { std::fill_n(begin(), size(), __u); }
77 { std::swap_ranges(begin(), end(), __other.begin()); }
82 { return iterator(&_M_instance[0]); }
86 { return const_iterator(&_M_instance[0]); }
90 { return iterator(&_M_instance[_Nm]); }
94 { return const_iterator(&_M_instance[_Nm]); }
98 { return reverse_iterator(end()); }
100 const_reverse_iterator
102 { return const_reverse_iterator(end()); }
106 { return reverse_iterator(begin()); }
108 const_reverse_iterator
110 { return const_reverse_iterator(begin()); }
114 size() const { return _Nm; }
117 max_size() const { return _Nm; }
120 empty() const { return size() == 0; }
124 operator[](size_type __n)
125 { return _M_instance[__n]; }
128 operator[](size_type __n) const
129 { return _M_instance[__n]; }
132 at(size_type __n) const
134 if (__builtin_expect(__n > _Nm, false))
135 std::__throw_out_of_range("array::at");
136 return _M_instance[__n];
142 if (__builtin_expect(__n > _Nm, false))
143 std::__throw_out_of_range("array::at");
144 return _M_instance[__n];
157 { return *(end() - 1); }
161 { return *(end() - 1); }
165 { return &_M_instance[0]; }
169 { return &_M_instance[0]; }
172 // Array comparisons.
173 template<typename _Tp, std::size_t _Nm>
175 operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
176 { return std::equal(__one.begin(), __one.end(), __two.begin()); }
178 template<typename _Tp, std::size_t _Nm>
180 operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
181 { return !(__one == __two); }
183 template<typename _Tp, std::size_t _Nm>
185 operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
187 return std::lexicographical_compare(__a.begin(), __a.end(),
188 __b.begin(), __b.end());
191 template<typename _Tp, std::size_t _Nm>
193 operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
194 { return __two < __one; }
196 template<typename _Tp, std::size_t _Nm>
198 operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
199 { return !(__one > __two); }
201 template<typename _Tp, std::size_t _Nm>
203 operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
204 { return !(__one < __two); }
206 // Specialized algorithms [6.2.2.2].
207 template<typename _Tp, std::size_t _Nm>
209 swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
210 { std::swap_ranges(__one.begin(), __one.end(), __two.begin()); }
212 // Tuple interface to class template array [6.2.2.5].
213 template<typename _Tp> class tuple_size;
214 template<int _Int, typename _Tp> class tuple_element;
216 template<typename _Tp, std::size_t _Nm>
217 struct tuple_size<array<_Tp, _Nm> >
218 { static const int value = _Nm; };
220 template<int _Int, typename _Tp, std::size_t _Nm>
221 struct tuple_element<_Int, array<_Tp, _Nm> >
222 { typedef _Tp type; };
224 template<int _Int, typename _Tp, std::size_t _Nm>
226 get(array<_Tp, _Nm>& __arr)
227 { return __arr[_Int]; }
229 template<int _Int, typename _Tp, std::size_t _Nm>
231 get(const array<_Tp, _Nm>& __arr)
232 { return __arr[_Int]; }
233 } // namespace std::tr1